import { URL_NEWS_LAST, URL_PRESS_NEWS, URL_TRANSACTIONS_CEX } from "@/comm/api";
import { get } from "@/comm/request";
import { defineStore } from "pinia";

interface IState {
    lastDate: string;
    news: boolean;
    hasClick: boolean;
}

export const useAnn = defineStore("ann", {
    state: (): IState => ({
        lastDate: "",
        news: false,
        hasClick: false
    }),

    getters: {
        show(state) {
            return state.news && !state.hasClick;
        }
    },
    actions: {
        async init() {
            let res: any = await this.loadData();
            this.lastDate = res;
            setInterval(() => {
                this.check();
            }, 30000);
        },
        async loadData() {
            let res: any = await get(URL_PRESS_NEWS + "/page", { contentType: 1, size: 10, current: 1 });
            let ary = res.data.records || [];
            ary.sort((a: any, b: any) => (a.date > b.date ? -1 : 1));
            console.log("是否最新", ary)
            if (ary.length) {
                // this.lastDate = ary[0].date;
                return ary[0].date;
            }
            return null;
        },
        async check() {
            let res: any = await get(URL_NEWS_LAST);
            console.log(res, "最后的新闻时间");
            this.news = res.data != this.lastDate;
        },
        async click() {
            this.hasClick = true;
        }
    }
});